<template>
  <div class="forgot-password-view">
    <div class="forgot-password-container">
      <!-- 蓝色头部区域 -->
      <div class="forgot-password-header">
        <div class="header-decoration">
          <div class="circle circle-1"></div>
          <div class="circle circle-2"></div>
        </div>
        <h1 class="game-title">找回密码</h1>
        <p class="game-subtitle">Reset Password</p>
      </div>
      
      <!-- 白色表单区域 -->
      <div class="forgot-password-form">
        <!-- 步骤1: 验证用户信息 -->
        <div v-if="currentStep === 1" class="step-content">
          <el-form 
            :model="validateForm" 
            :rules="validateRules" 
            ref="validateFormRef" 
            label-position="top"
          >
            <el-form-item label="邮箱地址" prop="email">
              <el-input 
                v-model="validateForm.email" 
                placeholder="请输入注册时的邮箱地址"
                :prefix-icon="Message"
                clearable
                size="large"
              />
            </el-form-item>
            
            <el-form-item label="手机号码" prop="phone">
              <el-input 
                v-model="validateForm.phone" 
                placeholder="请输入注册时的手机号码"
                :prefix-icon="Phone"
                clearable
                size="large"
              />
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                size="large" 
                style="width: 100%"
                @click="handleValidateUser"
                :loading="userStore.loading"
                :disabled="!isValidateFormValid"
              >
                <el-icon v-if="!userStore.loading"><Check /></el-icon>
                {{ userStore.loading ? '验证中...' : '验证用户信息' }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <!-- 步骤2: 重置密码 -->
        <div v-if="currentStep === 2" class="step-content">
          <el-form 
            :model="passwordForm" 
            :rules="passwordRules" 
            ref="passwordFormRef" 
            label-position="top"
          >
            <el-form-item label="新密码" prop="newPassword">
              <el-input 
                v-model="passwordForm.newPassword" 
                type="password"
                placeholder="请输入新密码"
                :prefix-icon="Lock"
                clearable
                size="large"
                show-password
              />
            </el-form-item>
            
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input 
                v-model="passwordForm.confirmPassword" 
                type="password"
                placeholder="请再次输入新密码"
                :prefix-icon="Lock"
                clearable
                size="large"
                show-password
              />
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                size="large" 
                style="width: 100%"
                @click="handleResetPassword"
                :loading="userStore.loading"
                :disabled="!isPasswordFormValid"
              >
                <el-icon v-if="!userStore.loading"><Key /></el-icon>
                {{ userStore.loading ? '重置中...' : '重置密码' }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <!-- 步骤3: 成功提示 -->
        <div v-if="currentStep === 3" class="step-content">
          <div class="success-content">
            <el-icon class="success-icon"><CircleCheck /></el-icon>
            <h3 class="success-title">密码重置成功！</h3>
            <p class="success-message">您的密码已经成功重置，请使用新密码登录。</p>
            <el-button 
              type="primary" 
              size="large" 
              @click="goToLogin"
            >
              返回登录
            </el-button>
          </div>
        </div>
        
        <!-- 导航链接 -->
        <div class="forgot-password-links">
          <el-link 
            type="primary" 
            @click="goToLogin"
            class="back-to-login"
          >
            <el-icon><ArrowLeft /></el-icon>
            返回登录
          </el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  Message, 
  Phone, 
  Check, 
  Lock, 
  Key, 
  CircleCheck, 
  ArrowLeft 
} from '@element-plus/icons-vue'
import { useUserStore } from '../stores/user'

const router = useRouter()
const userStore = useUserStore()

// 当前步骤
const currentStep = ref(1)

// 表单引用
const validateFormRef = ref()
const passwordFormRef = ref()

// 验证用户信息表单
const validateForm = reactive({
  email: '',
  phone: ''
})

// 重置密码表单
const passwordForm = reactive({
  newPassword: '',
  confirmPassword: ''
})

// 表单验证状态
const isValidateFormValid = computed(() => 
  validateForm.email.trim() && validateForm.phone.trim()
)

const isPasswordFormValid = computed(() => 
  passwordForm.newPassword.trim() && 
  passwordForm.confirmPassword.trim() && 
  passwordForm.newPassword === passwordForm.confirmPassword
)

// 验证用户信息表单规则
const validateRules = {
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码格式', trigger: 'blur' }
  ]
}

// 重置密码表单规则
const passwordRules = {
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 处理验证用户信息
const handleValidateUser = async () => {
  try {
    await validateFormRef.value.validate()
    
    const validateData = {
      email: validateForm.email.trim(),
      phone: validateForm.phone.trim()
    }
    
    await userStore.validateUserInfo(validateData)
    currentStep.value = 2
  } catch (error) {
    console.error('验证用户信息失败:', error)
    // 错误消息已经在store中处理
  }
}

// 处理重置密码
const handleResetPassword = async () => {
  try {
    await passwordFormRef.value.validate()
    
    const forgetData = {
      email: validateForm.email.trim(),
      phone: validateForm.phone.trim(),
      newPassword: passwordForm.newPassword,
      confirmPassword: passwordForm.confirmPassword
    }
    
    await userStore.forgetPassword(forgetData)
    currentStep.value = 3
  } catch (error) {
    console.error('重置密码失败:', error)
    // 错误消息已经在store中处理
  }
}

// 返回登录页面
const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.forgot-password-view {
  min-height: 100vh;
  height: 100vh;
  background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.forgot-password-container {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  max-width: 450px;
  width: 100%;
  margin: 0 auto;
  position: relative;
}

/* 蓝色头部区域 */
.forgot-password-header {
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  padding: 40px 30px 30px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.header-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 80px;
  height: 80px;
  top: -20px;
  left: -20px;
}

.circle-2 {
  width: 60px;
  height: 60px;
  top: -10px;
  right: -10px;
}

.game-title {
  color: white;
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  text-align: center;
}

.game-subtitle {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  margin: 0;
  font-weight: 400;
  text-align: center;
}

/* 白色表单区域 */
.forgot-password-form {
  padding: 40px 30px;
  text-align: center;
}

.step-content {
  margin-bottom: 24px;
}

.success-content {
  text-align: center;
  padding: 20px 0;
}

.success-icon {
  font-size: 64px;
  color: #52c41a;
  margin-bottom: 16px;
}

.success-title {
  color: #52c41a;
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 12px 0;
}

.success-message {
  color: #606266;
  font-size: 16px;
  line-height: 1.5;
  margin: 0 0 24px 0;
}

:deep(.el-form) {
  text-align: center;
}

:deep(.el-form-item) {
  text-align: center;
  margin-bottom: 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
  margin-bottom: 8px;
  text-align: center;
  display: block;
}

:deep(.el-form-item__content) {
  text-align: center;
  display: flex;
  justify-content: center;
}

:deep(.el-input) {
  width: 100%;
  max-width: 350px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #1890ff;
}

:deep(.el-button) {
  border-radius: 8px;
  font-weight: 500;
  height: 48px;
  transition: all 0.3s ease;
  width: 100%;
  max-width: 350px;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  border: none;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(24, 144, 255, 0.3);
}

:deep(.el-button--primary:disabled) {
  background: #c0c4cc;
  transform: none;
  box-shadow: none;
}

/* 导航链接 */
.forgot-password-links {
  margin-top: 24px;
  text-align: center;
}

.back-to-login {
  font-size: 14px;
  text-decoration: none;
  text-align: center;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

@media (max-width: 768px) {
  .forgot-password-view {
    padding: 10px;
  }
  
  .forgot-password-container {
    margin: 10px auto;
  }
  
  .forgot-password-header {
    padding: 30px 20px 20px;
  }
  
  .forgot-password-form {
    padding: 30px 20px;
  }
  
  .game-title {
    font-size: 28px;
  }
  
  .game-subtitle {
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .forgot-password-view {
    padding: 10px;
  }
  
  .forgot-password-header {
    padding: 25px 15px 15px;
  }
  
  .forgot-password-form {
    padding: 25px 15px;
  }
  
  .game-title {
    font-size: 24px;
  }
}
</style> 